package com.team.ai.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.team.ai.entity.Content;
import com.team.ai.entity.ResultEntity;
import com.team.ai.entity.User;
import com.team.ai.service.AiListService;
import com.team.ai.service.ContentService;

import com.team.ai.exception.BusinessException;
import com.team.ai.exception.BusinessExceptionEnum;

import com.team.ai.service.util.Result;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@CrossOrigin//跨域
@RestController//遵循RestFul
@RequestMapping("v2/Content/")
public class ContentController {
    private static final Logger logger = LoggerFactory.getLogger(ContentController.class);//日志getLogger需要传入的类名

    @Resource
    ContentService contentService;

    /**
     * 保存用户提出的问题
     */
//    @SaCheckLogin
//    @PostMapping("insert")
//    public String insert(
//            @RequestParam(value = "text",defaultValue = "-1") String text,
//            @RequestParam(value = "daan",defaultValue = "-1") String daan
//    ){
//        Integer userId = StpUtil.getLoginIdAsInt();
//        return contentService.insert(text,daan,userId);
//    }
    @SaCheckLogin
    @PostMapping("addq")
    public String addq(
            @RequestParam String text,
            @RequestParam int aiId,
            @RequestParam String pic
    ){
        int userId = StpUtil.getLoginIdAsInt();
        return ResultEntity.success(contentService.addq(text,userId,aiId,pic));
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @SaCheckLogin
    @DeleteMapping("/delete")
    public Result delete(@RequestParam(value = "id",defaultValue = "-1") int id){

        Integer userId = StpUtil.getLoginIdAsInt();
        //检查地址
        Content content = contentService.selectByPrimaryKey(id);
        if (!userId.equals(content.getUserid())) {
            throw new BusinessException(BusinessExceptionEnum.REQUEST_FORBIDEN_DATA_NOT_YOUR);
        }
        //删除
        if (contentService.delete(id)>0) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 查询详情
     * @param id
     * @return
     */
    @SaCheckLogin
    @GetMapping("getDetail")
    public Result getRDetail(@RequestParam(value = "id",defaultValue = "-1") int id){
        Integer userId = StpUtil.getLoginIdAsInt();
        //查询数据，并检查
        Content content = contentService.selectByPrimaryKey(id);
        if (!userId.equals(content.getUserid())) {
            throw new BusinessException(BusinessExceptionEnum.REQUEST_FORBIDEN_DATA_NOT_YOUR);
        }
        if (contentService.selectByPrimaryKey(id)!=null){
            return Result.ok();
        }
        return Result.error();
    }


    @SaCheckLogin
    @GetMapping("list")
    public String selectList(
            @RequestParam(value = "aiId",defaultValue = "-1") int aiId
    ){
        Integer userId = StpUtil.getLoginIdAsInt();
        return contentService.selectList(userId,aiId);
    }


//    @Resource
//    textService textService;
//    @SaCheckLogin
//    @PostMapping("text")
//    private String text(@RequestParam String q){
//        try{
//            return String.valueOf(textService.send(q));
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return "";
//    }
//    @SaCheckLogin

}
